package com.nsoft.html5npub.utils;

import java.util.List;

import com.nsoft.html5npub.business.BookDeleter;
import com.nsoft.html5npub.business.BookDownloader;
import com.nsoft.html5npub.objects.BookItem;
import com.nsoft.html5npub.R;
import com.nsoft.html5npub.ViewActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

@SuppressWarnings("deprecation")
public class BookListAdapter extends ArrayAdapter<BookItem>{
	private final Context context;
	/**
	private Bitmap[] bmps;
	*/
	public BookListAdapter(Context ctx,
			/**Bitmap[] _bmps,*/List<BookItem> list){
		
		super(ctx,R.layout.booklist_row,list);
		this.context=ctx;
		/**
		this.bmps=_bmps;
		*/
	}
	
	@SuppressLint("UseValueOf")
	@Override
	public View getView(int position, View convertView, 
									ViewGroup parent){
		LayoutInflater inflater=(LayoutInflater)context
				.getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
		
		BookItem book=engine.getBookItem(position);
		
		View rowView=inflater.inflate(
				R.layout.booklist_row,parent,false);
		TextView textView=(TextView)
					rowView.findViewById(R.id.label);
		ImageView imageView=(ImageView)
					rowView.findViewById(R.id.logo);
		ImageButton openBtn=(ImageButton)rowView.
				findViewById(R.id.bookOpenButton);
		openBtn.setOnClickListener(
				new OpenClickListener(rowView));
		ImageButton downloadBtn=(ImageButton)rowView.
				findViewById(R.id.bookDownloadButton);
		downloadBtn.setOnClickListener(
				new DownloadClickListener(
						book,position));
		ImageButton deleteBtn=(ImageButton)rowView.
				findViewById(R.id.bookDeleteButton);
		deleteBtn.setOnClickListener(
			new DeleteClickListener(book,position));
		
		
		
		textView.setText(book.getContentName());
		boolean isBookDownloaded=new Boolean(
				book.getDownloaded().toUpperCase());
		openBtn.setEnabled(isBookDownloaded);
		deleteBtn.setEnabled(isBookDownloaded);
		
		/**
		 * load in different thread
		 */
		new LoadImageThread(imageView,position).execute();
		
		imageView.setAlpha(isBookDownloaded?255:150);
		
		return rowView;
	}
	
	
	private class OpenClickListener implements 
						View.OnClickListener{
		private View rowView;
		public OpenClickListener(View rv){
			this.rowView=rv;
		}
		
		@Override
		public void onClick(View v) {
			ListView bookLv=(ListView)rowView.getParent();
			int pos=bookLv.getPositionForView(rowView);
			
			Intent intent=new Intent(
					context,ViewActivity.class);
			intent.putExtra(
					Constant.booklist.BOOK_INDEX_TRANSFER,
					pos);
			context.startActivity(intent);
		}
		
	}
	
	private class DeleteClickListener implements
						View.OnClickListener{
		private BookItem book;
		private int position;
		
		public DeleteClickListener(
					BookItem _book,int pos){
			this.book=_book;
			this.position=pos;
		}
		
		@Override
		public void onClick(View v) {
			AlertDialog.Builder alert=
					new AlertDialog.Builder(context);
			alert.setIcon(R.drawable.ic_help);
			alert.setTitle(Constant.DIALOG_CONFIRM).
				setMessage(Constant.booklist.BLF_CFM_DOWNLOAD).
				setPositiveButton(Constant.DIALOG_YES,
					new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(
								DialogInterface dialog, 
								int which) {
							try{
								new BookDeleter(
									BookListAdapter.this.context,
									BookListAdapter.this,
									DeleteClickListener.this.book,
									DeleteClickListener.this.position).
								execute();
							}catch(Exception e){
								Log.v("BookArrayAdapter.DeleteClickListener",
										e.getClass()+": "+e.getMessage());
							}
						}
				}).
				setNegativeButton(Constant.DIALOG_NO,
					new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(
								DialogInterface dialog, 
								int which){
							dialog.cancel();
						}
					}).
				create().show();
		}
	}
	
	private class DownloadClickListener implements
				View.OnClickListener{
		private BookItem book;
		private int pos;
		
		public DownloadClickListener(BookItem book,int pos){
			this.book=book;
			this.pos=pos;
		}
		
		@Override
		public void onClick(View v) {
			AlertDialog.Builder alert=
					new AlertDialog.Builder(context);
			alert.setIcon(R.drawable.ic_help);
			alert.setTitle(Constant.DIALOG_CONFIRM).
				setMessage(Constant.booklist.BLF_CFM_DOWNLOAD).
				setPositiveButton(Constant.DIALOG_YES,
					new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(
								DialogInterface dialog, 
								int which) {
							
							if (!utils.isOnline(BookListAdapter.
											this.context)){
								utils.showError(BookListAdapter.
									this.context,Constant.
									dataserver.ERROR_NETWORK_TITLE,
									Constant.dataserver.
										ERROR_NETWORK_UNAVAILABLE);
								return;
							}
							
							new BookDownloader(
									BookListAdapter.this.context,
									BookListAdapter.this,
									DownloadClickListener.this.book,
									DownloadClickListener.this.pos).
								execute();
						}
				}).
				setNegativeButton(Constant.DIALOG_NO,
					new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(
								DialogInterface dialog, 
								int which){
							dialog.cancel();
						}
					}).
				create().show();
		}
	}
	
	


}
